package com.jk.controller;

import com.jk.dao.RecordDao;
import com.jk.pojo.RecordBean;
import com.jk.service.RecordService;
import com.jk.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName AlipayController
 * @Descripttion TODO
 * @Author DELL
 * @Date 2022/10/20 19:19
 * @Version 0.001
 */
@RestController
public class AlipayController {

    @Autowired
    private AlipayUtils aliPayUtil;



    @Autowired
    private RecordService recordService;

    //支付方法
    @RequestMapping("aliPay")
    public String aliPay(String total_amount, String subject, String body, HttpServletResponse httpResponse,Integer userid,String orderid,String out_trade_no) throws IOException {
        Integer record_id = userid;
        RecordBean recordById = recordService.findRecordById(record_id);
        //OrderBean orderForm = orderServiceApi.findOrderForm(userid, orderid);
        //付款金额，必填（动态的就选择动态获取）
        total_amount = recordById.getCost().toString();
        //订单名称，必填
        if (recordById.getRecord_state()==1){
            //订单名称，必填
            subject = "0-6岁儿童服务包";
        }else if (recordById.getRecord_state()==2){
            subject = "老年人服务包";
        }else if(recordById.getRecord_state()==3){
            subject = "慢性病护理包";
        }

        //订单号
        out_trade_no = userid.toString();
        //商品描述，可空
        body = "尊敬的"+recordById.getRecord_name()+"你好，您购买了"+subject;
        System.out.println(total_amount+"======"+subject+"======"+out_trade_no+"====="+body);
        aliPayUtil.alipay(total_amount,subject,body,httpResponse,out_trade_no,userid);
        return "购买成功！！！";
    }
}
